using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.Adapters;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using System.IO;
using Microsoft.SharePoint.WebControls;

namespace GetSharePointToValidate.Adapters
{
    public class GSPTV_WebPartZone : ControlAdapter
    {
        PlaceHolder _placeHolder;
        WebPartManager _wpm;

        protected override void OnInit(EventArgs e)
        {
            _wpm = WebPartManager.GetCurrentWebPartManager(this.Page);
            _placeHolder = new PlaceHolder();
            base.OnInit(e);
        }
        protected override void Render(HtmlTextWriter writerout)
        {
            try
            {
                WebPartZone webPartZone = base.Control as WebPartZone;
                if (SPContext.Current.FormContext.FormMode.Equals(SPControlMode.Display))
                {
                    string layoutOrientationStyle = string.Empty;

                    if (webPartZone.LayoutOrientation == Orientation.Vertical)
                        layoutOrientationStyle = "Vertical";
                    else
                        layoutOrientationStyle = "Horizontal";

                    foreach (WebPart webpart in webPartZone.WebParts)
                    {
                        StringBuilder stringBuilder = new StringBuilder();
                        using (StringWriter stringWriter = new StringWriter(stringBuilder))
                        {
                            using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
                            {

                                string chromeStyle = string.Empty;
                                string webpartStyle = string.Empty;

                                switch (webpart.ChromeType)
                                {
                                    case PartChromeType.Default:
                                        chromeStyle = "ChromeDefault";
                                        break;
                                    case PartChromeType.TitleOnly:
                                        chromeStyle = "ChromeTitleOnly";
                                        break;
                                    case PartChromeType.TitleAndBorder:
                                        chromeStyle = "ChromeTitleAndBorder";
                                        break;
                                    case PartChromeType.BorderOnly:
                                        chromeStyle = "ChromeBorderOnly";
                                        break;
                                    case PartChromeType.None:
                                        chromeStyle = "ChromeNone";
                                        break;
                                }
                                webpartStyle = string.Format("{0} {1}", chromeStyle, layoutOrientationStyle);
                                writer.AddAttribute(HtmlTextWriterAttribute.Class, webpartStyle);
                                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                                if (webpart.ChromeType == PartChromeType.TitleOnly || webpart.ChromeType == PartChromeType.TitleAndBorder || webpart.ChromeType == PartChromeType.Default)
                                {
                                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "WebpartHeadline");
                                    writer.RenderBeginTag(HtmlTextWriterTag.H2);
                                    writer.Write(webpart.Title);
                                    writer.RenderEndTag();
                                }
                                webpart.RenderControl(writer);
                                writer.RenderEndTag();

                                _placeHolder.Controls.Add(new LiteralControl(stringBuilder.ToString()));
                            }
                        }
                    }
                    _placeHolder.RenderControl(writerout);
                }
                else
                    base.Render(writerout);
            }
            finally
            {
                _wpm.Dispose();
            }
        }
    }
}
